<template>
  <el-dialog class="carDetail" width="1000px" :visible.sync="visible" title="查看详情" :close-on-click-modal="false" :close-on-press-escape="false">
    <div class="detailList" v-if="dataForm.length">
      <div class="infoH">车辆编号：{{dataForm.carNumber}}</div>
      <div class="dBody">
        <div class="left">
          <photo-dialog ref="photoDialog" :imgList="dataForm.picture"/>
        </div>
        <div class="right">
          <div class="item" v-for="(item, index) in headerList" :key="index">
            <span class="label">{{item.label}}:</span>
            <span class="value">{{dataForm[item.key]}}</span>
          </div>
          <el-date-picker
            v-model="appointmentTime"
            type="datetime"
            value-format="timestamp"
            placeholder="选择日期时间">
          </el-date-picker>
          <el-button type="primary" @click="appointment">预约</el-button>
        </div>
      </div>
    </div>
    <login-or-register ref="loginOrRegister"/>
  </el-dialog>
</template>

<script>
import photoDialog from './photoDialog.vue'
import Cookies from 'js-cookie'
import loginOrRegister from './loginOrRegister.vue'
export default {
  components: { photoDialog, loginOrRegister },
  data () {
    return {
      visible: false,
      dataForm: {},
      appointmentTime: '',
      headerList: [{
        key: 'carModel',
        label: '车型',
        width: '150'
      },
      {
        key: 'guidePrice',
        label: '厂商指导价',
        width: '100'
      },
      {
        key: 'manufacturer',
        label: '厂商',
        width: '100'
      },
      {
        key: 'level',
        label: '级别',
        width: '100'
      },
      {
        key: 'energyType',
        label: '能源类型',
        width: '100'
      },
      {
        key: 'timeMarket',
        label: '上市时间',
        width: '100'
      },
      {
        key: 'engine',
        label: '发动机',
        width: '100'
      },
      {
        key: 'maxPower',
        label: '最大功率',
        width: '100'
      },
      {
        key: 'maxTorque',
        label: '最大扭矩',
        width: '100'
      },
      {
        key: 'transmissionCase',
        label: '变速箱',
        width: '100'
      },
      {
        key: 'length',
        label: '长度',
        width: '100'
      },
      {
        key: 'wide',
        label: '宽度',
        width: '100'
      },
      {
        key: 'height',
        label: '高度',
        width: '100'
      },
      {
        key: 'carStructure',
        label: '车身结构',
        width: '100'
      },
      {
        key: 'maxSpeed',
        label: '最高车速',
        width: '100'
      },
      {
        key: 'accelerationTime',
        label: '官方百公里加速时间(s)',
        width: '100'
      },
      {
        key: 'stock',
        label: '车辆库存',
        width: '100'
      }]
    }
  },
  methods: {
    // 显示弹窗
    init (item) {
      this.visible = true
      this.dataForm = item
    },
    // 登陆或注册
    loginOrRegister () {
      this.$refs.loginOrRegister.init()
    },
    // 预约
    appointment () {
      console.log(Cookies.get('token'))
      if (this.appointmentTime) {
        if (Cookies.get('token')) {
          this.$http.post('/app/subscribe', {
            carInfoId: this.dataForm.id,
            appointmentTime: this.appointmentTime
          }).then(({ data: res }) => {
            let { code, msg } = res
            console.log(code, msg)
            if (code === 401) {
              this.loginOrRegister()
            }
            if (code === 0) {
              this.$message.success(msg)
            }
            if (code !== 0) {
              this.$message.error(msg)
            }
          })
        } else {
          this.loginOrRegister()
        }
      } else {
        return this.$message.error('先选择时间！')
      }
    }
  }
}
</script>
<style lang="scss">
.carDetail{
  box-shadow: 0px 0px 7px rgba(0, 0, 0, 0.12);
  border-radius: 8px;
  .el-dialog__header{
    .el-dialog__title{
      font-size: 14px;
      font-weight: bold;
    }
    border-bottom: 1px solid #f2f2f2;
  }
  .el-dialog__body{
    padding: 15px 20px;
  }
  .detailList{
    font-size: 14px;
    margin-top: 10px;
    .infoH{
      font-size: 18px;
      font-weight: bold;
      margin-bottom: 20px;
    }
    .dBody{
      display: flex;
      justify-content: space-around;
      .left{
        width: 70%;
      }
      .right{
        widows: 30%;
        .item{
          margin-bottom: 10px;
          .label{
            color: #999999;
            margin-right: 5px;
          }
        }
      }
    }
  }
}
</style>
